package plantshop.view.listeners;
import java.awt.Point;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.event.MouseInputListener;

import plantshop.GameStarter;
import plantshop.model.Client;
import plantshop.view.PlayerMenu;


public class PlayerMouseListener implements MouseInputListener, Serializable {
	private static final long serialVersionUID = -6058841155310919950L;
	private PlayerMenu menu;
	private JFrame frame;
	private boolean gender;
	private Map<String,BufferedImage> images;
	private Map<String, BufferedImage[]> animations;
	private boolean canStart = false;
	
	public PlayerMouseListener(PlayerMenu menu,JFrame frame,Map<String,BufferedImage> images, Map<String, BufferedImage[]> animations){
		this.menu = menu;
		this.frame = frame;
		this.images = images;
		this.animations = animations;
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		// Nothing
	}
	@Override
	public void mousePressed(MouseEvent e) {
		// Nothing

	}
	@Override
	public void mouseReleased(MouseEvent e) {
		Point p = e.getPoint();
		int[] maleCoords = menu.getMaleCoords();
		int[] femaleCoords = menu.getFemaleCoords();
		int[] nameCoords = menu.getNameCoords();
		int[] ipCoords = menu.getIPCoords();
		
		
		//If they click on one of the characters
		if((p.x >= maleCoords[0] && p.x <= maleCoords[0]+maleCoords[2]) &&(p.y >= maleCoords[1] && p.y <= maleCoords[1]+maleCoords[3])){
			gender = true;
			canStart = true;
		}
		else if((p.x >= femaleCoords[0] && p.x <= femaleCoords[0]+femaleCoords[2]) &&(p.y >= femaleCoords[1] && p.y <= femaleCoords[1]+femaleCoords[3])){
			gender = false;
			canStart = true;
		}
		
		//Otherwise do nothing
		
		//Register keylistener for IP address
		if(menu.isMultiplayer()){
			if((p.x >= ipCoords[0] && p.x <= ipCoords[0] + ipCoords[2]) && (p.y >= ipCoords[1] && p.y <= ipCoords[1] + ipCoords[3])){
				List<KeyListener> kls = Arrays.asList(menu.getKeyListeners());
				for(KeyListener kl:kls){
					menu.removeKeyListener(kl);
				}
				menu.requestFocusInWindow();
				menu.addKeyListener(new IPListener(menu));
			}
		}
		
		//Register keylistener for Name
		//Moved this to when the player menu got created in MenuMouseListener as well so you don't have to click the box to type at first
		if((p.x >= nameCoords[0] && p.x <= nameCoords[0] + nameCoords[2]) && (p.y >= nameCoords[1] && p.y <= nameCoords[1] + nameCoords[3])){
			List<KeyListener> kls = Arrays.asList(menu.getKeyListeners());
			for(KeyListener kl:kls){
				menu.removeKeyListener(kl);
			}
			menu.requestFocusInWindow();
			menu.addKeyListener(new NameListener(menu));
		}
		
		//Start the game if the player has been chosen
		if(canStart){
			if(menu.isIP() && menu.isMultiplayer()){
				//Start a new game with the desired IP for the Server
				String name = menu.getPlayerName();
				if(name != ""){
					frame.getContentPane().removeAll();
					try {
						new Client(menu.getIPAddress(), name, true, images, frame, animations);
					} catch (IOException e1) {
					}
				}
			}
			else if(!menu.isMultiplayer()){
				String name = menu.getPlayerName();
				if(name != ""){
					//Start a new game with the Player
					frame.getContentPane().removeAll();
					GameStarter.runTutorial(images, frame, name, gender, animations);
				}
				else{
					menu.setNeedsName(true);
					menu.repaint();
				}
			}
			else{
				//Set player gender and get the IP before starting game
				menu.setNeedsIP(true);
				menu.repaint();
			}
		}
		
	}
	@Override
	public void mouseEntered(MouseEvent e) {
	}
	
	@Override
	public void mouseExited(MouseEvent e) {
	}
	
	@Override
	public void mouseDragged(MouseEvent e) {
	}
	
	@Override
	public void mouseMoved(MouseEvent e) {
	}
}
